package Classes.GameViews.ViewPanels
{
	import Classes.GameDatas.GD_Item;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Shop;
	import Classes.GameUtils.GU_MsgDeal;
	import Classes.GameUtils.GU_NewThingsControler;
	import Classes.GameUtils.GU_NewThingsInfo;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameUtils.MyTipSprite;
	import Classes.GameUtils.ToolTip_Sprite;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.ViewDisplayers.MapTargets.MapMoveTargets.MMT_Character;
	import Classes.GameViews.ViewIcons.ViewIcon;
	import Classes.GameViews.ViewScenes.VS_Main;
	import Classes.Interfaces.I_SendTarget;
	import Classes.Managers.M_Communication;
	import Classes.Managers.M_ConfigData;
	import Classes.staticinfo.Language;
	import Classes.staticinfo.PlatformInfo;
	
	import MyComponent.MyButton;
	
	import UIComponents.FlatInfo;
	import UIComponents.IGD_ShopItem;
	import UIComponents.PanelEvent;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.ApplicationDomain;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 *快速购买类 
	 * @author 邓坚
	 */
	public class VP_FastBuy_actPoint extends ViewPanel  implements I_SendTarget
	{
		private static var _instance:VP_FastBuy_actPoint;
		private var _interfaceUI:Sprite;
		private var _shopItem1:IGD_ShopItem;
		private var _shopItem2:IGD_ShopItem;
		private var _nowShop:IGD_ShopItem;
		public function VP_FastBuy_actPoint()
		{
			init();
		}
		
		public function init():void
		{
			_interfaceUI	=  	new (ApplicationDomain.currentDomain.getDefinition("FastBuy_actPanel_v") as Class);
			this.addChild(_interfaceUI);
			addListener();
		}
		
		public function addListener():void
		{
			this.addEventListener(PanelEvent.CLOSE,closeFun);
			this.addEventListener("usebtn1Click",usebtn1Click);
			this.addEventListener("usebtn2Click",usebtn2Click);
			this.addEventListener("btnC1Click", btnC1Click);
			this.addEventListener("btnC2Click", btnC2Click);
		}
		
		/**
		 *使用左边 
		 * @param e
		 * 
		 */
		private function usebtn1Click(e:Event):void
		{
			//			this._nowShop	=	this._shopItem1;
			
			//			var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
			//			M_Communication.instance.send(12303,msgContent);
			
			new GU_NewThingsControler(GU_NewThingsInfo.instance.getNewThingsByType(5));
			this.closeFun(null);
			//			
			//			if(isClose)
			//				this.closeFun(null);
		}
		
		/**
		 *使用右边 
		 * @param e
		 * 
		 */
		private function usebtn2Click(e:Event):void
		{
			this._nowShop	=	this._shopItem2;
			var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
			M_Communication.instance.send(12303,msgContent);
			
			this.closeFun(null);
			
		}
		
		/**
		 *购买左边 
		 * @param e
		 * 
		 */
		private function btnC1Click(e:Event):void
		{
			this._nowShop	=	this._shopItem1;
			
			if(M_ConfigData.instance.isOpenQQVip && this._nowShop.moneyType==0)
			{
				PlatformInfo.instance.qqPlatBuyItemFun(this._nowShop.shopItemType,this._nowShop.shopItemImg,1,(_nowShop.itemName+"*"+_nowShop.itemName),_nowShop.shopItemId);
				
			}else{
				
				if(!this._shopItem1.isReachMoney){		//没有足够的钱
					VP_errorPanel.instance.setErrorMessage("没有足够的钱");
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
					this.closeFun(new Event(""));
					return;
				}
				
				if(this._shopItem1){
					var msgContent1:Object={itemType:this._shopItem1.shopItemType,pil:1};
					M_Communication.instance.send(12100,msgContent1,this);
					this.closeFun(new Event(""));
				}
			}
		}
		
		/**
		 *购买右边 
		 * @param e
		 */
		private function btnC2Click(e:Event):void
		{
			this._nowShop	=	this._shopItem2;
			
			if(M_ConfigData.instance.isOpenQQVip && this._nowShop.moneyType==0)
			{
				PlatformInfo.instance.qqPlatBuyItemFun(this._nowShop.shopItemType,_nowShop.shopItemImg,1,(_nowShop.itemName+"*"+_nowShop.itemName),_nowShop.shopItemId);
				
			}else{
				
				if(!this._nowShop.isReachMoney){		//没有足够的钱
					VP_errorPanel.instance.setErrorMessage("没有足够的钱");
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
					this.closeFun(new Event(""));
					return;
				}
				
				if(this._nowShop){
					var msgContent1:Object={itemType:this._nowShop.shopItemType,pil:1};
					M_Communication.instance.send(12100,msgContent1,this);
					this.closeFun(new Event(""));
				}
			}
		}
		
		/**
		 *设置求助，左边 
		 * 
		 */
		public function setHelp():void
		{
			setTip(this.interfaceUI["spPic1"] as DisplayObject, Language.ActPointLeftTipHelp);
		}
		
		
		/**
		 *设置右边物品 
		 * @param itemId
		 * 
		 */
		public function setShopRight(itemId:uint):void
		{
			var tipString:String	=	"";
			_shopItem2=GD_Shop.instance.getShopInfoByItemInfo(itemId);
			if(_shopItem2==null){
				this.closeFun(null);
				return;
			}
			
			var shop1:IGD_ShopItem		=	GD_Shop.instance.getShopInfoByItemInfo(10410001);	//薯条
			var shop2:IGD_ShopItem		=	GD_Shop.instance.getShopInfoByItemInfo(10410002);	//炸鸡块
			var shop3:IGD_ShopItem		=	GD_Shop.instance.getShopInfoByItemInfo(10410003);	//汉堡包
			
			var isHave:Boolean	=	false;
			if( !isHave	&& GD_Player.instance.items.hasOwnProperty( shop1.shopItemId) && (GD_Player.instance.items[shop1.shopItemId] as GD_Item).itemNumber>0)
			{
				this._shopItem2		=	shop1;						//薯条
				isHave=true;
			}
			if( !isHave	&& GD_Player.instance.items.hasOwnProperty( shop2.shopItemId) && (GD_Player.instance.items[shop2.shopItemId] as GD_Item).itemNumber>0)
			{
				this._shopItem2		=	shop2;						//炸鸡块
				isHave=true;
			}
			if( !isHave	&& GD_Player.instance.items.hasOwnProperty( shop3.shopItemId) && (GD_Player.instance.items[shop3.shopItemId] as GD_Item).itemNumber>0)
			{
				this._shopItem2		=	shop3;						//汉堡包
				isHave=true;
			}
			
			
			var vi:ViewIcon		=	_shopItem2.shopItemPic as ViewIcon;
			vi	=	new ViewIcon(vi.sourceName, vi.picName);
			vi.iWidth	=	80;
			vi.iHeight	=	80;
			vi.setNumSpVisiable(false);
			//			(this._interfaceUI["spPic2"] as ViewIcon).release();
			var sp:Sprite	=	this._interfaceUI["spPic2"];
			while( sp.numChildren)
			{
				if(sp.getChildAt(0) as ViewIcon)
				{
					(sp.getChildAt(0) as ViewIcon).release();
				}else{
					sp.removeChildAt(0);
				}
			}
			this._interfaceUI["spPic2"]	=	vi;		
			var tf:TextField =	new TextField();
			
			//			trace( (_shopItem2.shopItemPic as ViewIcon).sourceName,(_shopItem2.shopItemPic as ViewIcon).picName);
			//tf.mouseEnabled	=	_txt2.mouseEnabled	=	false;
			
			tf.text		=	"花费："+(this._shopItem2.moneyType==0?"欧币:":"金币:")+this._shopItem2.price+"\n购买"+this._shopItem2.itemName+this._shopItem2.perNum;
			tf.wordWrap	=	true;
			tf.autoSize	=	TextFieldAutoSize.CENTER;
			
			if(isHave){
				//热感
				tipString	=	this._shopItem2.itemName+":"+this._shopItem2.itemDes;//Language.ActPointRightTipUse;
				setTip(this.interfaceUI["spPic2"] as DisplayObject, tipString);
				
				this._interfaceUI["btnContainerRight"].visible=false;
				(this._interfaceUI["useBtnRight"] as MyButton).visible=true;
				//				this._interfaceUI.addChild( (this._interfaceUI.getChildByName("btn1") as MyButton));
			}
			else{
				
				
				this._interfaceUI["btnContainerRight"].visible=true;
				
				(this._interfaceUI["useBtnRight"] as MyButton).visible=false;
				
				(this._interfaceUI["buyBtnRight"] as MyButton).buttonMode	 = (this._interfaceUI["btnContainerRight"] as DisplayObjectContainer).mouseEnabled 	=	this._shopItem2.isReachMoney;
				
				var mtype:String="jb";
				if(this._shopItem2.moneyType==0){
					if(M_ConfigData.instance.isOpenQQVip)
						mtype	=	"qb";
					else
						mtype	=	"ob";
				}
				
				this._interfaceUI["setMoenyIcon2"](mtype);
				this._interfaceUI["itemPrice2"]=this._shopItem2.price;
				
				//热感
				tipString	=	Language.ActPointRightTipBuy;
				tipString	=	tipString.replace( "@JbType", (this._shopItem2.moneyType==0?(FlatInfo.isOPenQQVip?Language.Qdian:Language.ob):Language.jb));
				tipString	=	tipString.replace( "@JbNum", this._shopItem2.price);
				tipString	=	tipString.replace( "@Title", this._shopItem2.itemName);
				tipString	=	tipString.replace( "@Num", this._shopItem2.perNum);	
				
				setTip(this.interfaceUI["spPic2"] as DisplayObject, tipString);
				setTip(this.interfaceUI["btnContainerRight"] as DisplayObject, tipString);
			}
		}
		
		
		private function closeFun(e:Event):void
		{
			GV_Map.instance.buildMode	=	GV_Map.CANNOTBUILDMODE;
			this.close();
		}
		
		public static function get instance():VP_FastBuy_actPoint
		{
			if(!_instance) _instance=new VP_FastBuy_actPoint;
			return _instance;
		}
		
		public function get interfaceUI():Sprite
		{
			return _interfaceUI;
		}
		
		public function openPanel():void
		{
			var mtype:String;
			if(M_ConfigData.instance.isOpenQQVip)
				mtype	=	"qb";
			else
				mtype	=	"ob";
			this._interfaceUI["setMoenyIcon2"](mtype);
			this._interfaceUI["itmePrice2"]=100;
			
			this.setHelp();
			this.setShopRight(10410001);
			
			//			if( ( (GV_Map.instance.controlTarget as MMT_Character).actTarget is MN_Zombie ) )
			//			if( ((GV_Map.instance.controlTarget as MMT_Character).actTarget as MN_Zombie ).actionName!="die")
			GV_Map.instance.buildMode	=	GV_Map.MOVEMODE;
			this.openAt(VS_Main.instance,true);
			this.toCenter();
		}
		
		
		/**
		 * 检查行动点 
		 * @return 0控制对像有行动力，1好友只有一行动 ，2玩家只有一行动, 3好友没行动力，4玩家没行动力，
		 * 
		 */
		public function check():uint
		{
			var returnV:uint=0;
			var act:Number	=	0;
			
			if( ( GV_Map.instance.controlTarget as MMT_Character).characterData.characterState==2){
				act 	=	GD_Player.instance.friendAct;
				if(act<1){
					VP_errorPanel.instance.setErrorMessage("友情行动力不足！");
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
					return 3;
				}
			}
			else
			{
				//				if( ( GV_Map.instance.controlTarget as MMT_Character).characterData.characterState == 2){
				//					
				//				}
				act		=	(GV_Map.instance.controlTarget as MMT_Character).characterData.gd_characterPlay.actPoint;
			}
			
			if(act < 1 ){
				returnV=3;
				
				if( ( GV_Map.instance.controlTarget as MMT_Character).characterData.characterState == 1){
					VP_errorPanel.instance.setErrorMessage(Language.hiredFriendNoAct);
					VP_errorPanel.instance.enterFrame();
					GU_Sound.instance.playCommSound("System_Prompt");
					return 3;
				}
				else
				{
					this.openPanel();
					returnV=4;
				}
				//				if(GD_Player.instance.character.view == GV_Map.instance.controlTarget)
				//				{
				//					this.openPanel();
				//					returnV=4;
				//				}
			}
			else 
				if(act == 1){
					returnV=1;
					if(GD_Player.instance.character.view == GV_Map.instance.controlTarget)
						returnV =2;
				}
			
			//			trace(act,returnV, (GV_Map.instance.controlTarget as MMT_Character).actTarget, (GV_Map.instance.controlTarget as MMT_Character).actTarget is MN_Zombie);
			//			if( (GV_Map.instance.controlTarget as MMT_Character).actTarget is MN_Zombie )
			//				trace( ((GV_Map.instance.controlTarget as MMT_Character).actTarget as MN_Zombie).actionName);
			return returnV;
		}
		
		/**
		 *发送一个消息,返回的时候拦截 
		 * @param msgObje
		 * 
		 */
		public function dealMsg(dealObj:Object):void
		{		
			if(dealObj.msgType==22600)			//更改道具数量
			{
				var msgContent:Object={itemID:this._nowShop.shopItemId,friendId:0};
				M_Communication.instance.send(12303,msgContent);
			}
			
			(GU_MsgDeal.instance["dealMsg_"+dealObj.msgType] as Function)(dealObj);
		}
		
		/**
		 *设置热感 
		 * @param d
		 * @param st
		 * 
		 */
		public function setTip(d:DisplayObject, st:String):void
		{
			ToolTip_Sprite.getInstance().removeTip(d);
			ToolTip_Sprite.getInstance().setTip(d,new MyTipSprite(st),ToolTip_Sprite.DIRECTION_LEFT);
		}
		
	}
}